package logic

import (
	"context"
	"fmt"
	"supplier/models/mysql"

	"supplier/supplierrpc/internal/svc"
	"supplier/supplierrpc/supplierrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NewPasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNewPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NewPasswordLogic {
	return &NewPasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NewPasswordLogic) NewPassword(in *supplierrpc.NewPasswordRequest) (*supplierrpc.NewPasswordResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Where("account", in.Account).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("当前账号不存在")
	}

	users := mysql.User{
		Account:  in.Account,
		Password: in.Password,
	}
	mysql.DB.Debug().Updates(&users)

	return &supplierrpc.NewPasswordResponse{
		UserId: int64(user.ID),
	}, nil
}
